<template>
  <div class="shopcar-card">
    <div class="mui-card" v-for="item in cartList" :key="item.id">
      <div class="mui-card-content">
        <div class="mui-card-content-inner goods-item">
          <!-- 开关 -->
          <mt-switch v-model="selectedObj[item.id]" @change="changeSeleted(item.id,selectedObj[item.id])"></mt-switch>
          <!-- 图片 -->
          <img :src="item.thumb_path" alt />
          <!-- 信息区域 -->
          <div class="info">
            <h1>{{ item.title }}---</h1>
            <div class="goods_info">
              <span class="goods_price">
                ￥
                <span class="price">{{item.sell_price}}</span>
              </span>
              <nobox :initCount="countObj[item.id]" :id="item.id"></nobox>
              <a href="#" @click.prevent="del(item.id)">删除</a>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner count">
          <div class="left">
            <p>总计（不含邮费）</p>
            <p>
              已勾选商品
              <span class="danger">{{selectedcount}}</span>件，总价
              <span class="danger">￥{{amount}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import nobox from "../sub-component/shopcar_nobox.vue";
import { mapGetters, mapMutations } from "vuex";

export default {
  data() {
    return {
      cartList: []
    };
  },
  components: {
    nobox
  },
  created() {
    this.getCartList();
  },
  methods: {
    ...mapMutations(["deleteCart","selectedChange"]),
    async getCartList() {
      if (this.idstr.length <= 0) return;
      const { data } = await this.$http.get(
        "/api/goods/getshopcarlist/" + this.idstr
      );
      if (data.status === 0) {
        return (this.cartList = data.message);
      }
    },
    del(id) {
      this.cartList.some((item, i) => {
        if (item.id == id) {
          this.cartList.splice(i, 1);
          return true;
        }
      });
      this.deleteCart(id);
    },
    changeSeleted(id,flag){
      console.log(id,flag);
      this.selectedChange({id:id,selected:flag})
    }
  },
  computed: {
    ...mapGetters(["idstr", "countObj","selectedObj","selectedcount","amount"])
  }
};
</script>

<style lang="scss" scoped>
.goods-item {
  display: flex;
  img {
    width: 60px;
    height: 60px;
  }
  .info {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    h1 {
      font-size: 13px;
    }
    .goods_info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .goods_price {
        color: red;
        font-weight: 700;
      }
    }
  }
}

.danger {
  color: red;
  font-weight: 700;
  font-size: 16px;
}
.count {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>